package com.treeroot.designMode.Strategy.useFactory;

import com.treeroot.designMode.Strategy.DiscountStrategy;
import com.treeroot.designMode.Strategy.Goods;

/**
 * 由上下文对象根据不同的工厂类型来创建对应的策略对象
 * @author treeroot
 *
 */
public class DiscountContext {
    private final Goods goods;
    private DiscountStrategy strategy;

    public DiscountContext(Goods goods, DiscountFactory factory) {
        this.goods = goods;
        this.strategy = factory.createDiscountStrategy();
    }

    public double calculateDiscountPrice() {
        return this.strategy.calculateDiscountPrice(goods);
    }

    /**
     * 切换策略生成工厂
     * @param factory 工厂
     */
    public void setFactory(DiscountFactory factory) {
        this.strategy = factory.createDiscountStrategy();
    }
}
